package br.com.felix.bmt.web;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import br.com.felix.bmt.business.BlogManagement;
import br.com.felix.bmt.exception.ImpossibleToPostException;
import br.com.felix.bmt.model.Post;
import br.com.felix.bmt.model.User;
import br.com.felix.bmt.web.components.PostUi;
import br.com.felix.fwt.security.exception.UnauthorizedAccessException;
import br.com.felix.fwt.servlet.FelixWebToolkitServlet;
import br.com.felix.fwt.ui.ActionListener;
import br.com.felix.fwt.ui.Button;
import br.com.felix.fwt.ui.Form;
import br.com.felix.fwt.ui.HtmlWriter;
import br.com.felix.fwt.ui.Link;
import br.com.felix.fwt.ui.TextArea;
import br.com.felix.fwt.ui.VerticalPanel;
import br.com.felix.fwt.ui.css.Style;
import br.com.felix.fwt.ui.exception.UserInterfaceInitializationException;
import br.com.felix.fwt.ui.javascript.Message;

/**
 * "Page" that renders a user's Blog.
 * */
public class Blog extends BlogManagementForm implements Serializable{

	private static final long serialVersionUID = 807151580671770554L;
	
	private TextArea txtaNewPost = new TextArea();
	
	private User user;
	
	private boolean edit;
	
	public Blog(User usr, boolean edit){
		this.user = usr;
		this.edit = edit;
	}
	
	
	/**
	 * Sets the title, and render the posts and link to home. 
	 * */
	@Override
	protected void initializeUserInterface() throws UserInterfaceInitializationException {
		
		
		 //Vertical panel to display the Ui components vertically.
		VerticalPanel vpanel = new VerticalPanel();
		
		HtmlWriter htmlw = new HtmlWriter();
		String title = user.name;
		if(user.name.endsWith("s")){
			title+= "' blog";
		}
		else{
			title+= "'s blog";
		}
		htmlw.header1(title);
		vpanel.add(htmlw);
		
		
		Link lnkHome = new Link("change user");
		lnkHome.actionListener = new ActionListener("goToHome",true){
			@Override
			public Form actionPerformed() throws UserInterfaceInitializationException, UnauthorizedAccessException {
				return FelixWebToolkitServlet.getLoadedForm(UserSelection.class);
			}
		};
		vpanel.add(lnkHome);

		Style textAreaCssStyle = new Style().width(640).height(300); //define the text area's Css style.
		txtaNewPost.setStyle(textAreaCssStyle);
		
		if(edit){
		    vpanel.add(txtaNewPost);
		}
		
		
		BlogManagement bm = BlogManagement.instance(); //The business class that implements the Blog.
		List<Post> lstPosts = bm.getPosts(user); //Obtain the list of posts from the file.
		if(lstPosts!=null){
			for(Post p:lstPosts){
				vpanel.add(new PostUi(p)); //Add an interface component for each Post. 
			}
		}
		
		//Define the save button.
		String buttonLabel = "Save";
		Button btnSave = new Button(buttonLabel);
		String actionName = "save";
		
		//Define an action to be made when the button is clicked (and the html form submitted).
		btnSave.actionListener = new ActionListener(actionName){
			@Override
			public Form actionPerformed() throws UserInterfaceInitializationException, UnauthorizedAccessException {
				return saveNewPost();
			}
		};
		
		if(edit){
		    vpanel.add(btnSave);
		}
		add(vpanel);
	}
	
	

	/**
	 * Saves the user inputted post to the database. Makes empty content verification.
	 * */
	protected Form saveNewPost() throws UserInterfaceInitializationException {
		String newPostContent = txtaNewPost.getValue(); //obtain the value the user has inputted in the text area.
		
		if(newPostContent==null || newPostContent.length()==0){
			Message javascriptAlert = new Message("Content cannot be empty!");
			executeJavascriptOnce(javascriptAlert);
			return this;
		}
		
		Post p = new Post(user);
		p.setContent(newPostContent);
		Date today = new Date();
		p.setDate(today);
		
		BlogManagement bm = BlogManagement.instance();
		try {
			bm.post(p); //save the new post in the file
		}
		catch (ImpossibleToPostException e) {
			Message msg = new Message("Could not post. "+e.getMessage());
			executeJavascriptOnce(msg);
			return this;
		}
		
		childs.clear(); //clear the current interface.
		txtaNewPost.setValue(""); //clear the contents of the text area.
		initializeUserInterface(); //rebuild the interface. I know this is lazy.
		return this;
	}

}
